﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace socket客户端与服务端通信
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket? socketfwq;//服务端服务
        Dictionary<string, Socket> keyValuePairs = new Dictionary<string, Socket>(); //把客户端的保存
        /// <summary>
        /// 服务端
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                IPAddress ip = IPAddress.Any;
                Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint point = new IPEndPoint(IPAddress.Any, 1456);
                socketWatch.Bind(point);
                socketWatch.Listen(10);//最多接收10个客户端
                textBox3.AppendText("启动服务成功");
                byte[] buffer = new byte[1024];
                Task.Run(() =>
                {
                    while (true)
                    {
                        socketfwq = socketWatch.Accept();
                        keyValuePairs.Add(socketfwq.RemoteEndPoint.ToString(), socketfwq);
                        BeginInvoke(new Action(() =>
                        {
                            comboBox1.Items.Add(socketfwq.RemoteEndPoint.ToString());
                            comboBox1.SelectedItem = socketfwq.RemoteEndPoint.ToString();
                        }));
                        Task.Run(() =>
                        {
                            while (true)
                            {
                                int i = socketfwq.Receive(buffer);
                                string relust = Encoding.UTF8.GetString(buffer, 0, i);
                                BeginInvoke(new Action(() =>
                                {
                                    textBox3.AppendText("\r\n" + relust);
                                }));
                            }
                        });
                    }
                });
            }
            catch (Exception)
            {
            }
        }
        static Socket? ClientSocket;//客户端
        /// <summary>
        /// 客户端连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse("192.168.254.8");
            IPEndPoint endPoint = new IPEndPoint(ip, 1456);
            ClientSocket.Connect(endPoint);
            textBox2.AppendText("连接成功:" + ClientSocket.LocalEndPoint.ToString());
            ClientSocket.Send(Encoding.UTF8.GetBytes($"\r\n {ClientSocket.LocalEndPoint.ToString()}:客户端连接成功"));
            //comboBox1.Items.Add(ClientSocket.LocalEndPoint);
            byte[] buffer = new byte[1024];
            Task.Run(() =>
            {
                while (true)
                {
                    int i = ClientSocket.Receive(buffer);
                    BeginInvoke(new Action(() =>
                    {
                        textBox2.AppendText($"\r\n{ClientSocket.RemoteEndPoint}:" + Encoding.UTF8.GetString(buffer, 0, i));
                    }));
                }
            });
        }
        /// <summary>
        /// 客户端发送文字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            ClientSocket.Send(Encoding.UTF8.GetBytes($"{ClientSocket.LocalEndPoint.ToString()}:" + textBox1.Text));
            textBox2.AppendText($"\r\n {ClientSocket.LocalEndPoint.ToString()} :" + textBox1.Text);
        }
        /// <summary>
        /// 服务端发送文字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            keyValuePairs[comboBox1.SelectedItem.ToString()].Send(Encoding.UTF8.GetBytes(textBox4.Text));
            textBox3.AppendText($"\r\n {comboBox1.SelectedItem.ToString()} :" + textBox4.Text);
        }
    }
}



